<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="{{ url_for('static', filename='index.css') }}">
    <title>Login</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #F9F2E0;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .navbar {
            color: #F9F2E0;
            width: 100%;
            background-color: #1660AB;
            padding: 1rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        .navbar .title a {
            color: #F9F2E0;
            text-decoration: none;
            font-size: 1.5rem;
            font-weight: bold;
        }

        .input {
            width: 100%;
            padding: 0.75rem;
            margin: 0.5rem 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 1rem;
        }

        .button {
            width: 100%;
            padding: 0.75rem;
            margin-top: 1rem;
            background-color: #888888;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1rem;
        }

        .button:hover {
            background-color: #0056b3;
        }

        .alert {
            color: red;
            margin-top: 1rem;
        }
    </style>
</head>

<body>
    <div class="navbar">
        <span class="title"><a href="/" style="text-decoration: none;">Student Website</a></span>
    </div>

    <div class="card">
        <p class="title" style="color: #F9F2E0">Login</p>
        <label for="username" style="color: #F9F2E0">Username</label>
        <input type="text" id="username" name="username" class="input" required>
        <label for="password" style="color: #F9F2E0">Password</label>
        <input type="password" id="password" name="password" class="input" required>
        <button class="button" onclick="login()" style="color: #F9F2E0; font-size: 18px;">Login</button>
        <div id="alert" class="alert"></div>
    </div>

    <script>
        function login() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            fetch('/api/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username: username, password: password })
            })
                .then(response => {
                    if (response.ok) {
                        window.location.href = "/";
                    } else {
                        document.getElementById('alert').innerText = 'Incorrect username or password';
                    }
                })
                .catch(error => {
                    console.error('error:', error);
                    document.getElementById('alert').innerText = 'An error occurred. Please try again later.';
                });
        }
    </script>
</body>

</html>
